package com.ziyia.databasetest;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    private final String AUTHORITY = "com.ziyia.databasetest.provider";
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.ziyia.databasetest", appContext.getPackageName());

        Uri uri = Uri.parse("content://" + AUTHORITY + "/Book");
        Cursor query = appContext.getContentResolver().query(uri, null, null, null, null);

        System.out.println(uri);
        if (query != null) {
            while (query.moveToNext()) {
                String name = query.getString(query.getColumnIndex("name"));
                Log.d("query", name);
            }
        }

        String insert = insert(appContext);
        Log.d("insert", insert);
    }

    private String insert(Context context) {
        Uri uri = Uri.parse("content://com.ziyia.databasetest.provider/book");
        ContentValues values = new ContentValues();
        values.put("name", "A Clash of Kings");
        values.put("author", "George Martin");
        values.put("pages", 1040);
        values.put("price", 22.85);
        Uri newUri = context.getContentResolver().insert(uri, values);
        return newUri.getPathSegments().get(1);
    }



}